// 14位字符串数字转换成 年:月:日 时:分:秒
export function parseStr2DateStr(time) {
  if (arguments.length === 0 || !time) {
    return null;
  }
  if (time.length === 14) {
    const pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/;
    return time.replace(pattern, '$1-$2-$3 $4:$5:$6');
  }
  return time;
}

// 14位字符串数字转换成date
export function parseStr2Date(time) {
  if (arguments.length === 0 || !time) {
    return null;
  }
  if (time.length === 14) {
    new RegExp(/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/).exec(time);
    return new Date(
      RegExp.$1,
      RegExp.$2,
      RegExp.$3,
      RegExp.$4,
      RegExp.$5,
      RegExp.$6
    );
  }
  return time;
}

// 格式化当前时间为yyyyMMddHHmmss
export function parseTime2Str(date) {
  !date && (date = new Date());
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  month < 10 && (month = '0' + month);
  let day = date.getDate();
  day < 10 && (day = '0' + day);
  let hour = date.getHours();
  hour < 10 && (hour = '0' + hour);
  let minute = date.getMinutes();
  minute < 10 && (minute = '0' + minute);
  let second = date.getSeconds();
  second < 10 && (second = '0' + second);
  return year + month + day + +hour + minute + second;
}

// 格式化当前时间为yyyy-MM-dd HH:mm:ss
// date日期，
// timeZero：true 时分秒都为0
export function parseTime2DateStr(date, timeZero) {
  date ? '' : (date = new Date());
  new RegExp(/(\d{4}).(\d{1,2}).(\d{1,2})/).exec(date.toLocaleDateString());
  let year = RegExp.$1.length === 2 ? '00' + RegExp.$1 : RegExp.$1;
  let month = RegExp.$2.length === 1 ? '0' + RegExp.$2 : RegExp.$2;
  let day = RegExp.$3.length === 1 ? '0' + RegExp.$3 : RegExp.$3;
  return timeZero
    ? year + '-' + month + '-' + day + ' 00:00:00'
    : year + '-' + month + '-' + day + ' ' + date.toTimeString().substr(0, 8);
}

// 回显数据字典
export function selectDictLabel(datas, value) {
  var actions = [];
  Object.keys(datas).map(key => {
    if (datas[key].value == '' + value) {
      actions.push(datas[key].label);
      return false;
    }
  });
  return actions.join('');
}

/**
 * 判断浏览器是否支持shim技术，即支持webrtc
 */
export function isSupportedShim() {
  var navigator = window.navigator;
  var result = { browser: null, version: null };
  if (navigator.mozGetUserMedia) {
    // Firefox
    return true;
  } else if (
    navigator.webkitGetUserMedia ||
    (window.isSecureContext === false &&
      window.webkitRTCPeerConnection &&
      !window.RTCIceGatherer)
  ) {
    return true;
  } else if (
    navigator.mediaDevices &&
    navigator.userAgent.match(/Edge\/(\d+).(\d+)$/)
  ) {
    // Edge.
    return true;
  } else if (
    window.RTCPeerConnection &&
    navigator.userAgent.match(/AppleWebKit\/(\d+)\./)
  ) {
    // Safari.
    return true;
  }

  return false;
}

export function guid() {
  function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  }
  return (
    S4() +
    S4() +
    '-' +
    S4() +
    '-' +
    S4() +
    '-' +
    S4() +
    '-' +
    S4() +
    S4() +
    S4()
  );
}
